package com.mobisys.live.controller;

import com.mobisys.live.bean.usergroup.*;
import com.mobisys.live.dao.mapper.RUserGroupUserMapper;
import com.mobisys.live.dao.mapper.UserGroupMapper;
import com.mobisys.live.dao.mapper.UserMapper;
import com.mobisys.live.dao.model.RUserGroupUserKey;
import com.mobisys.live.dao.model.User;
import com.mobisys.live.dao.model.UserGroup;
import com.mobisys.live.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import static com.mobisys.live.config.ResponseCode.*;

/**
 * Created by zhuo on 2018-01-16.
 * 用户组管理相关的操作
 */
@RestController
public class UserGroupController {

    @Autowired
    private UserGroupMapper userGroupMapper;

    @Autowired
    private RUserGroupUserMapper rUserGroupUserMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取用户组列表
     * @param userId  用户组所属的用户的用户id
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/1.0/user_group/group/list", method = RequestMethod.GET)
    public Response getUserGroupList(@RequestParam String userId,@RequestParam int page,@RequestParam int size){
        Response<List<UserGroupBean>> response=new Response();

        //分页获取用户组列表，每页10个
        page=(page-1)*10;
        List<UserGroup> userGroupList=userGroupMapper.selectUserGroupByPage(userId,page,size,null);

        if(userGroupList==null){
            response.setRetureValue(GROUP_LIST_IS_EMPTY);
            response.setRetureMsg(MSG_GROUP_LIST_IS_EMPTY);
            return response;
        }
        List<UserGroupBean> list=new ArrayList<>();
        for (int i = 0; i < userGroupList.size(); i++) {
            UserGroup userGroup=userGroupList.get(i);
            UserGroupBean userGroupBean=new UserGroupBean();
            userGroupBean.setGroupOwnerUserId(userGroup.getUserId());
            userGroupBean.setGroupName(userGroup.getUserGroupName());
            userGroupBean.setCreateTime(userGroup.getCreateTime().getTime()+"");

            list.add(userGroupBean);
        }

        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(list);

        return response;
    }

    /**
     * 获取用户组详情
     * @param groupId
     * @return
     */
    @RequestMapping(value = "/1.0/user_group/group/{groupId}", method = RequestMethod.GET)
    public Response getUserGroupDetail(@PathVariable String groupId){
        Response response=new Response();

        UserGroup userGroup=userGroupMapper.selectByPrimaryKey(groupId);
        if(userGroup==null){
            response.setRetureValue(USER_GROUP_DOES_NOT_EXIST);
            response.setRetureMsg(MSG_USER_GROUP_DOES_NOT_EXIST);
            return response;
        }

        UserGroupBean userGroupBean=new UserGroupBean();
        userGroupBean.setGroupOwnerUserId(userGroup.getUserId());
        userGroupBean.setGroupName(userGroup.getUserGroupName());
        userGroupBean.setGroupName(userGroup.getCreateTime().getTime()+"");
        return response;
    }

    /**
     * 添加用户组
     * @return
     */
    @RequestMapping(value = "/1.0/user_group/group", method = RequestMethod.POST)
    public Response addUserGroup(@RequestBody UserGroupBean userGroupBean){
        Response response=new Response();

        //检查下是否当前用户组已存在，判断用户组名是否重复


        UserGroup userGroup=userGroupMapper.selectByUserGroupName(userGroupBean.getGroupName());
        if(userGroup!=null){
            //当前用户组名的用户已经存在了
            response.setRetureValue(USER_GROUP_ALREADY_EXIST);
            response.setRetureMsg(MSG_USER_GROUP_ALREADY_EXIST);
            return response;

        }else {
            userGroup=new UserGroup();
        }
        userGroup.setUserId(userGroupBean.getGroupOwnerUserId());
        userGroup.setUserGroupName(userGroupBean.getGroupName());
        userGroup.setCreateTime(new Date(userGroupBean.getCreateTime()));

        if(userGroupMapper.insertSelective(userGroup)>0){
            response.setRetureValue(SUCCESS);
            response.setRetureMsg(MSG_SUCCESS);
            return response;
        }else {

            response.setRetureValue(CREATE_USER_GROUP_ERROR);
            response.setRetureMsg(MSG_CREATE_USER_GROUP_ERROR);
            return response;
        }

    }


    /**
     * 批量删除用户组
     * @return
     */
    @RequestMapping(value = "/1.0/user_group/group/delete", method = RequestMethod.POST)
    public Response deleteUserGroup(@RequestBody DeleteGroupBean deleteGroupBean){
        Response response=new Response();

        List<String> idList=deleteGroupBean.getGroupIdList();
        if(idList==null){
            response.setRetureValue(INPUT_IS_INVALID);
            response.setRetureMsg(MSG_INPUT_IS_INVALID);
        }
        if(userGroupMapper.deleteUserGroup(idList)>0){
            response.setRetureValue(SUCCESS);
            response.setRetureMsg(MSG_SUCCESS);
            return response;
        }else {
            response.setRetureValue(DELETE_GROUP_ERROR);
            response.setRetureMsg(MSG_DELETE_GROUP_ERROR);
            return response;
        }
    }



    /**
     * 添加用户到指定用户组里（支持批量添加）
     * @return
     */
    @RequestMapping(value = "/1.0/user_group/user", method = RequestMethod.POST)
    public Response addUsersToUserGroup(@RequestBody GroupUserBean addUserBean){
        Response response=new Response();

        String groupId=addUserBean.getGroupId();
        List<String> userIdList=addUserBean.getUserIdList();
        for (int i = 0; i < userIdList.size(); i++) {
            RUserGroupUserKey rUserGroupUserKey=new RUserGroupUserKey();
            rUserGroupUserKey.setUserGroupId(groupId);
            rUserGroupUserKey.setUserId(userIdList.get(i));
            if(rUserGroupUserMapper.insert(rUserGroupUserKey)<=0){
                response.setRetureValue(ADD_USER_TO_GROUP_ERROR);
                response.setRetureMsg(MSG_ADD_USER_TO_GROUP_ERROR);
                return response;
            }

        }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);

        return response;
    }

    /**
     * 导入已有用户组到当前用户组
     * @param importGroupBean
     * @return
     */
    @RequestMapping(value = "/1.0/user_group/user/import", method = RequestMethod.POST)
    public Response importUserGroupsToUserGroup(@RequestBody ImportGroupBean importGroupBean) {
        Response response=new Response();


        return response;
    }


    /**
     * 批量删除用户组里的用户
     *
     * @return
     */
    @RequestMapping(value = "/1.0/user_group/user/delete", method = RequestMethod.POST)
    public Response deleteUsersFromUserGroup(@RequestBody GroupUserBean groupUserBean ){
        Response response=new Response();


        if(rUserGroupUserMapper.deleteUsersFromGroup(groupUserBean.getGroupId(),groupUserBean.getUserIdList())>0){
            response.setRetureValue(SUCCESS);
            response.setRetureMsg(MSG_SUCCESS);
            return  response;
        }else {
            response.setRetureValue(DELETE_USER_FROM_GROUP_ERROR);
            response.setRetureMsg(MSG_DELETE_USER_FROM_GROUP_ERROR);
            return response;
        }

    }
    /**
     * 搜索单个用户加入用户组
     *
     * @param userparam
     * @return
     */
    @RequestMapping(value = "/1.0/user_group/user/search", method = RequestMethod.GET)
    public Response SelectUser(@RequestParam String userparam) {
        Response response = new Response();
        List<User> userList ;
        // 规定正则表达式，确定传来的参数是username或者是nickname
        String regex = "[1-9][0-9]{3,11}";//第一位从1开始，第二位从0到9，重复3到11次

        if(userparam.matches(regex)){
            //传入的是username
            userList = userMapper.selectByUserNameLike(userparam);
        }else{
            //传入的是nickname
            userList = userMapper.selectByUNickNameLike(userparam);
        }
        if(userList.isEmpty()){
            response.setRetureValue(LIST_IS_EMPTY);
            response.setRetureMsg(MSG_LIST_IS_EMPTY);
            return response;
        }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(userList);
        return response;

    }


    /**
     * 获取用户组里的用户，分页获取
     *
     *
     * @return
     */
    @RequestMapping(value = "/1.0/user_group/user/{groupId}", method = RequestMethod.GET)
    public Response getUsersFromUserGroupByPage(@PathVariable String groupId,@RequestParam int page,
    @RequestParam int size){
        Response response=new Response();



        return response;
    }


    /**
     * 按筛选条件获取用户列表，分页获取
     * @param filterConditionBean
     * @return
     */
    @RequestMapping(value = "/1.0/user_group/user/", method = RequestMethod.POST)
    public Response getUsersFromUserGroupByCondition(@RequestBody FilterConditionBean filterConditionBean){
        Response response=new Response();

        filterConditionBean.setSize(10);
        int offset=filterConditionBean.getOffset();
        filterConditionBean.setOffset((offset-1)*10);

        List<User> userList=userMapper.selectUsersByCondition(filterConditionBean);
        if(userList==null){
            response.setRetureValue(USER_LIST_EMPTY);
            response.setRetureMsg(MSG_USER_LIST_EMPTY);
            return response;
        }



        return response;
    }


}
